package com.cjyd.hrjb.controller.backend;

import com.cjyd.hrjb.controller.ApiGroup;
import com.cjyd.hrjb.controller.BaseBackendController;
import com.cjyd.hrjb.model.dto.AdminDto;
import com.cjyd.hrjb.model.dto.ResponseDto;
import com.cjyd.hrjb.model.entity.Admin;
import com.cjyd.hrjb.service.AdminService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created fei wo by 2020/4/7
 */
@RestController
@RequestMapping(ApiGroup.BACKEND_V1)
public class AdminManageController extends BaseBackendController {

    private AdminService adminService;

    public AdminManageController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/login")
    public ResponseDto<Admin> login(@RequestBody AdminDto adminDto) {
        return success(adminService.login(adminDto));
    }

    @PostMapping("/admin")
    public ResponseDto<Admin> add(@RequestBody AdminDto adminDto) {
        return success(adminService.add(adminDto));
    }

    @PutMapping("/admin")
    public ResponseDto<Admin> edit(@RequestBody AdminDto adminDto) {
        return success(adminService.edit(adminDto));
    }

    @DeleteMapping("/admin/{id}")
    public ResponseDto<Void> delete(@PathVariable("id") Long id) {
        adminService.delete(id);
        return success();
    }

    @GetMapping("/admins")
    public ResponseDto<List<Admin>> list() {
        return success(adminService.list());
    }

    @PostMapping("/logout")
    public ResponseDto<Void> logout(HttpServletRequest servletRequest) {
        String token = servletRequest.getHeader("token");
        adminService.logout(token);
        return success();
    }

}
